package com.jinlejia.erp.ui.common;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;

import com.jinlejia.erp.R;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 底部级联选择框
 */

public class SheetWheel extends AlertDialog {
    private ArrayList<Map<String,Object>> listMapsS;
    private ArrayList<Map<String,Object>> listMapsT;
    private ArrayList<Map<String,Object>> listMapsW;
    private ArrayList<Map<String,Object>> listMapsC;
    private Context context;
    ArrayList<String> dataS = new ArrayList<>();
    ArrayList<String>dataT = new ArrayList<>();
    ArrayList<String> dataW = new ArrayList<>();
    ArrayList<String> dataC = new ArrayList<>();

    public SheetWheel(Context c,ArrayList<Map<String, Object>> listMapsS, ArrayList<Map<String, Object>> listMapsT, ArrayList<Map<String, Object>> listMapsW, ArrayList<Map<String, Object>> listMapsC) {
        super(c);
        this.context = c;
        this.listMapsS = listMapsS;
        this.listMapsT = listMapsT;
        this.listMapsW = listMapsW;
        this.listMapsC = listMapsC;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_sheet_wheel);
        initdata();
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        int fountSize = 30;
        int margin = 35;

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, (int) (d.getHeight() * 0.4));

        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#333333");
        style.textColor = Color.GRAY;
        style.selectedTextSize = 20;
        style.holoBorderColor = Color.GRAY;

        final WheelView wv1 = window.findViewById(R.id.wheelview1);
        wv1.setWheelAdapter(new ArrayWheelAdapter(context));
        wv1.setWheelData(dataS);
        wv1.setLoop(true);
        wv1.setSkin(WheelView.Skin.Holo);
        wv1.setStyle(style);

        wv1.setExtraText("室", Color.parseColor("#333333"), fountSize, margin);

        final WheelView wv2 = window.findViewById(R.id.wheelview2);
        wv2.setWheelAdapter(new ArrayWheelAdapter(context));
        wv2.setSkin(WheelView.Skin.Holo);
        wv2.setWheelData(dataT);
        wv2.setLoop(true);
        wv2.setStyle(style);
        wv2.setExtraText("厅", Color.parseColor("#333333"), fountSize, margin);

        final WheelView wv3 = window.findViewById(R.id.wheelview3);
        wv3.setWheelAdapter(new ArrayWheelAdapter(context));
        wv3.setSkin(WheelView.Skin.Holo);
        wv3.setWheelData(dataC);
        wv3.setLoop(true);
        wv3.setStyle(style);
        wv3.setExtraText("厨", Color.parseColor("#333333"), fountSize, margin);

        final WheelView wv4 = window.findViewById(R.id.wheelview4);
        wv4.setWheelAdapter(new ArrayWheelAdapter(context));
        wv4.setSkin(WheelView.Skin.Holo);
        wv4.setWheelData(dataW);
        wv4.setLoop(true);
        wv4.setStyle(style);
        wv4.setExtraText("卫", Color.parseColor("#333333"), fountSize, margin);


        window.findViewById(R.id.cancel).setOnClickListener(v -> dismiss());

        window.findViewById(R.id.confirm).setOnClickListener(v -> {
            if( null != selectListener ){
                Map<String,Integer> map = new HashMap<>();
                map.put("room",Integer.parseInt((String)wv1.getSelectionItem()));
                map.put("hall",Integer.parseInt((String)wv2.getSelectionItem()));
                map.put("kitchen",Integer.parseInt((String)wv3.getSelectionItem()));
                map.put("toilet",Integer.parseInt((String)wv4.getSelectionItem()));
                selectListener.select(map);
            }
            dismiss();
        });

    }

    private void initdata() {
        for (Map<String,Object> map : listMapsS) {
            Object valueName = map.get("valueName");
            dataS.add((String) valueName);
        }


        for (Map<String,Object> map : listMapsT) {
                Object valueName = map.get("valueName");
                dataT.add((String) valueName);

        }


        for (Map<String,Object> map : listMapsC) {
                Object valueName = map.get("valueName");
                dataC.add((String) valueName);
        }


        for (Map<String,Object> map : listMapsW) {
                Object valueName = map.get("valueName");
                dataW.add((String) valueName);
        }
    }

    public interface OnSelectListener {
        void select(Map<String, Integer> map);
    }

    private OnSelectListener selectListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }

}
